<?php
error_reporting(0);
	/*
	* 订单
	*/
	class order{
		
		public $t;
		public $PageSize=10;
		
		
		/*订单列表*/
		function getOrderList($s){
			global $db;
			$where="1=1 ";
			$begDateS = Post('begDate');
			$endDateS = Post('endDate');
			$nameS = Post('name');
			$begDate=strtotime(empty($begDateS) ? '' : $begDateS);
			$endDate=strtotime(empty($endDateS) ? '' : $endDateS);
			$name=empty($nameS) ? '' : $nameS;
			if($s){
				$where.=" and Tord_status=$s";	
			}
			if($begDate){
				$where.=" and Tord_createtime > $begDate";	
			}
			if($endDate){
				$where.=" and Tord_createtime < $endDate";	
			}
			if($name){
				$where.=" and (per_name like '%".$name."%' or per_phone like '%".$name."%' or Tord_id='{$name}')";	
			}
			$sql="select * from ticket_order where ".$where." and Tord_cst_id=".$_SESSION['login']['userId']." order by Tord_createtime desc";
			$total=$db->QueryCount($sql);
			$this->t=array('total'=>$total,'perpage'=>$this->PageSize,'style'=>'a2');
			$tmpPage=$_GET['PageNum']!="" ? intval($_GET['PageNum']):0;
			$tmpPageSize=$tmpPage==0 ? 0 : ($tmpPage-1)*$this->PageSize;
			$sql .= ' limit '.$tmpPageSize.','.$this->PageSize ;
			$rs = $db->GetAllSql($sql);
			return $rs;
		}
		
		/*订单详细*/
		function getOrderDetail($id){
			global $db;
			$sql="select * from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			return $rs;		
		}
		
		/*下订单*/
		function sendOrder(){
			global $db;
			$data=$_POST['data'];
			$data['Tord_createtime']=time();
			$data['Tord_cst_id']=$_SESSION['login']['userId'];
			$data['Tord_status']=1;
			$data['Tord_playdate']=strtotime($data['Tord_playdate']);
			$shipping=$_POST['shipping'];
			if($data['Tord_num']==''){
				Alert_Url('请填写预订数量','order.php?aid='.$data['Tord_att_id'].'&id='.$data['Tord_tkt_id']);		
			}
			if($data['Tord_price2']==''){
				Alert_Url('请填写你的销售价格','order.php?aid='.$data['Tord_att_id'].'&id='.$data['Tord_tkt_id']);		
			}
			if($data['per_name']==''){
				Alert_Url('请填写游客姓名','order.php?aid='.$data['Tord_att_id'].'&id='.$data['Tord_tkt_id']);		
			}
			
			//dump($db->InsertRow('ticket_order',$data));
			if($id=$db->InsertRow('ticket_order',$data)){
				$this->ordUpdateNum($id);
				$_SESSION['flag']=0;
				Location("order.php?act=sendOrdPay&ordid=$id");
			}else{
				Alert_back('订单生产失败');	
			}
			
		}
		
		/*订单状态和操作*/
		function getStatus($s){
			switch($s){
				case "7":
					$status='<span class="num_color">已取消</span>';
					break;
				case "6":
					$status='<span class="num_color">未能游玩<span>';
					break;
				case "5":
					$status='<p class="t4 t_0" style="color:#0080C0;">已成功，已支付，已确认，已出票</p>';
					break;
				case "4":
					$status='<p class="t4 t_0" style="color:#0080C0;">已成功，已支付，已确认，已出票</p><p class="t4 t_0">待游玩</p>';
					break;
				case "3":
					$status='<p class="t4 t_0" style="color:#0080C0;">已成功，已支付，已确认</p><p class="t4 t_0">待出票</p>';
					break;
				case "2":
					$status='<p class="t4 t_0" style="color:#0080C0;">已成功，已支付</p><p class="t4 t_0">待确认</p>';
					break;
				default:
					$status='<p class="t4 t_0" style="color:#0080C0;">已成功，<span class="num_color">未支付</span></p>';
					break;
			}
			return $status;
		}
		
		/*根据订单状态获取订单数量*/
		function getOrderNum($s=''){
			global $db;
			$where = '';
			if($s){
				$where=" and Tord_status=$s";	
			}
			$sql="select count(*) as num from ticket_order where Tord_cst_id=".$_SESSION['login']['userId'].$where;
			$rs=$db->GetOneSql($sql);
			return $rs['num'];	
		}
		/*更新门票销售数量*/
		function ordUpdateNum($id){
			global $db;
			$sql="select Tord_tkt_id,Tord_num from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			$sql2="select Tkt_sellnum,Tkt_id from ticket where Tkt_id=".$rs['Tord_tkt_id'];
			$rs2=$db->GetOneSql($sql2);
			$sellnum=$rs2['Tkt_sellnum']+$rs['Tord_num'];
			$sql3="update ticket set Tkt_sellnum=$sellnum where Tkt_id=".$rs2['Tkt_id'];
			$db->query($sql3);
		}
		
		/*预订成功后立即支付*/
		function ordpay($id,$allPrice){
			global $db;	
			$sql="select * from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			
			$tsql="select * from ticket where Tkt_id=".$rs['Tord_tkt_id'];
			$trs=$db->GetOneSql($tsql);
			if($trs['Tkt_shipping']==0){
				//取得短信模板
				$content=getMsgContent2($id,1);
				//dump($content);
				//取得手机号码
				$sql="select per_phone from ticket_order where Tord_id='".$id."'";
				$phone=$db->GetOneVal("per_phone",$sql);
				//发送短信
				if(!empty($phone) && !empty($content))
				{
					sendMsg($phone,$content);
				}
			}
			if($rs['Tord_status']==1){
				$uinfo=getUserInfo();//用户信息
				$bh=$uinfo['mem_money']-$allPrice;
				mysql_query("BEGIN");
	
				//更新账户余额
				$ycrs=$db->query("update member_merchant set mem_money=$bh where mem_id=".$_SESSION['login']['userId']);
				
				//更新订单状态
				$ddrs=$db->query("update ticket_order set Tord_status=2 where Tord_id=$id");
				
				//添加财务日志
				$data=array(
					'Fin_name'=>'支付订单号'.$id.',支付总金额'.$allPrice.'元',
					'Fin_type'=>1,
					'Fin_add_amount'=>$allPrice,
					'Fin_amount_after_change'=>$bh,
					'Fin_cst_id'=>$_SESSION['login']['userId'],
					'Fin_createtime'=>time(),
					'Fin_content'=>'支付订单号'.$id.',支付总金额'.$allPrice.'元'
				);
				$log=$db->InsertRow('fin_log',$data);
				
				if($ycrs and $ddrs and $log){
					mysql_query("COMMIT");
					echo js('alert("支付成功");');
					echo js('window.parent.frames["head"].location.reload();');
					Location("order.php?act=list&s=2");
	
				}else{
					mysql_query("ROLLBACK");
					echo js('alert("支付失败");');
					echo js('window.parent.frames["head"].location.reload();');
					Location("order.php?act=list&s=1");
	
				}
			}else{
				echo js('alert("订单已经支付");');
				echo js('window.parent.frames["head"].location.reload();');
				Location("order.php?act=list&s=".$rs['Tord_status']);	
			}
		}
		
		
		/*到未支付订单里去支付*/
		function pay($id,$allPrice){
			global $db;	
			
			$sql="select * from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			
			$tsql="select * from ticket where Tkt_id=".$rs['Tord_tkt_id'];
			$trs=$db->GetOneSql($tsql);
			if($trs['Tkt_shipping']==0){
				//取得短信模板
				$content=getMsgContent2($id,1);
				//dump($content);
				//取得手机号码
				$sql="select per_phone from ticket_order where Tord_id='".$id."'";
				$phone=$db->GetOneVal("per_phone",$sql);
				//发送短信
				if(!empty($phone) && !empty($content))
				{
					sendMsg($phone,$content);
				}
			}
			
			
			$uinfo=getUserInfo();//用户信息
			$bh=$uinfo['mem_money']-$allPrice;
			mysql_query("BEGIN");

			//更新账户余额
			$ycrs=$db->query("update member_merchant set mem_money=$bh where mem_id=".$_SESSION['login']['userId']);
			
			//更新订单状态
			$ddrs=$db->query("update ticket_order set Tord_status=2 where Tord_id=$id");
			
			//添加财务日志
			$data=array(
				'Fin_name'=>'支付订单号'.$id.',支付总金额'.$allPrice.'元',
				'Fin_type'=>1,
				'Fin_add_amount'=>$allPrice,
				'Fin_amount_after_change'=>$bh,
				'Fin_cst_id'=>$_SESSION['login']['userId'],
				'Fin_createtime'=>time(),
				'Fin_content'=>'支付订单号'.$id.',支付总金额'.$allPrice.'元'
			);
			$log=$db->InsertRow('fin_log',$data);
			
			if($ycrs and $ddrs and $log){
				mysql_query("COMMIT");
				echo js('alert("支付成功");');
				echo js('frameElement.dg.reload();');
				echo js('window.parent.parent.frames["head"].location.reload();');

			}else{
				mysql_query("ROLLBACK");
				echo js('alert("支付失败");');
				echo js('frameElement.dg.reload();');
				echo js('window.parent.parent.frames["head"].location.reload();');

			}
			
		}
		
		/*撤销订单*/
		function cancelOrder($id){
			global $db;
			$sqlf="select * from ticket_order where Tord_id=$id";
			$rsf=$db->GetOneSql($sqlf);
			if($rsf['Tord_status']!=7){
				$sql="update ticket_order set Tord_status=7 where Tord_id=$id";
				if($db->query($sql)){
					$sql2="select * from ticket_order where Tord_id=$id";
					$rs2=$db->GetOneSql($sql2);
					if($rsf['Tord_status']!=1){
						
						$sql3="select * from ticket where Tkt_id=".$rs2['Tord_tkt_id'];
						$rs3=$db->GetOneSql($sql3);
						
						if($rs3['Tkt_shipping']==1){
							$cost=$rs2['Tord_num']*$rs2['Tord_price3']+$rs3['Tkt_freight'];//邮寄方式的包含运费
						}else{
							$cost=$rs2['Tord_num']*$rs2['Tord_price3'];	
						}
						$uinfo=getUserInfo();
						$costzj=$cost+$uinfo['mem_money'];//余额+退款
						
						$sql4="update member_merchant set mem_money=$costzj where mem_id=".$_SESSION['login']['userId'];
						if($db->query($sql4)){
							//添加财务日志
							$data=array(
								'Fin_name'=>'撤销订单号'.$id.',退还总金额'.$cost.'元',
								'Fin_type'=>0,
								'Fin_add_amount'=>$cost,
								'Fin_amount_after_change'=>$costzj,
								'Fin_cst_id'=>$_SESSION['login']['userId'],
								'Fin_createtime'=>time(),
								'Fin_content'=>'撤销订单号'.$id.',退还总金额'.$cost.'元'
							);
							$db->InsertRow('fin_log',$data);
							echo js('alert("订单撤销成功");');
							echo js('window.parent.frames["head"].location.reload();');
							echo js('location.href="order.php?act=list&s=7"');	
						}else{
							echo js('alert("金额退还失败");');
							echo js('window.location.reload();');	
						}
					}else{
						echo js('alert("订单撤销成功");');
						echo js('window.parent.frames["head"].location.reload();');
						echo js('location.href="order.php?act=list&s=7"');		
					}
					
				}else{
					echo js('alert("订单撤销失败");');
					echo js('window.location.reload();');
				}
			}else{
				echo js('alert("订单已经撤销");');
				echo js('location.href="order.php?act=list&s=7"');	
			}
		}
		
		/*修改订单*/
		function modifyOrder(){
			global $db;
			$id=$_POST['id'];
			$data=$_POST['data'];
			$s=$_POST['s'];
			$data['Tord_playdate']=strtotime($data['Tord_playdate']);
			$sql="select * from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			$sql2="select * from ticket where Tkt_id=".$rs['Tord_tkt_id'];
			$rs2=$db->GetOneSql($sql2);
			if($rs2['Tkt_shipping']==1){
				$oldPrice=$rs['Tord_num']*$rs2['Tkt_price3']+$rs2['Tkt_freight'];	
			}else{
				$oldPrice=$rs['Tord_num']*$rs2['Tkt_price3'];	
			}
			if($s==2){//账户月不足的情况下
				$uinfo=getUserInfo();//用户信息
				if($rs['Tord_num'] < $data['Tord_num']){
					$xgnum=$data['Tord_num']-$rs['Tord_num'];//增加的数量
					$xgprice=$xgnum*$rs2['Tkt_price3'];
					if($xgprice>$uinfo['mem_money']){
						echo js('alert("账户余额不足，请先充值");');
						echo js('window.close();');
						exit();		
					}
				}	
			}	
			if($db->updateRow('ticket_order',$data,"Tord_id=$id")){
				if($s!=1){
					$this->modifyPay($oldPrice,$id);
				}
				echo js('alert("订单修改成功");');
				echo js('window.close();');	
			}else{
				Alert_back('订单修改失败');
				
			}
		}
		
		/*订单修改支付*/
		function modifyPay($oldPrice,$id){
			global $db;
			$sql="select * from ticket_order where Tord_id=$id";
			$rs=$db->GetOneSql($sql);
			$sql2="select * from ticket where Tkt_id=".$rs['Tord_tkt_id'];
			$rs2=$db->GetOneSql($sql2);
			if($rs2['Tkt_shipping']==1){
				$nowPrice=$rs['Tord_num']*$rs2['Tkt_price3']+$rs2['Tkt_freight'];	
			}else{
				$nowPrice=$rs['Tord_num']*$rs2['Tkt_price3'];
			}
			$sql3="select * from member_merchant where mem_id=".$_SESSION['login']['userId'];
			$rs3=$db->GetOneSql($sql3);
			
			if($nowPrice > $oldPrice){
				$newPrice=$nowPrice-$oldPrice;//需要再支付的金额
				
				$bh=$rs3['mem_money']-$newPrice;
				$data=array(
					'Fin_name'=>'支付订单号'.$id.',支付总金额'.$newPrice.'元',
					'Fin_type'=>1,
					'Fin_add_amount'=>$newPrice,
					'Fin_amount_after_change'=>$bh,
					'Fin_cst_id'=>$_SESSION['login']['userId'],
					'Fin_createtime'=>time(),
					'Fin_content'=>'支付订单号'.$id.',支付总金额'.$newPrice.'元'
				);
			}else{
				$newPrice=$oldPrice-$nowPrice;//需要退还的金额
				$bh=$rs3['mem_money']+$newPrice;
				$data=array(
					'Fin_name'=>'订单号'.$id.',修改订单退还总金额'.$newPrice.'元',
					'Fin_type'=>0,
					'Fin_add_amount'=>$newPrice,
					'Fin_amount_after_change'=>$bh,
					'Fin_cst_id'=>$_SESSION['login']['userId'],
					'Fin_createtime'=>time(),
					'Fin_content'=>'订单号'.$id.',修改订单退还总金额'.$newPrice.'元'
				);
			}
			
			$db->query("update member_merchant set mem_money=$bh where mem_id=".$_SESSION['login']['userId']);
			//添加财务日志
			$db->InsertRow('fin_log',$data);
		}
		
		
		/*成交率*/
		function getTurnoverRate(){
			global $db;
			$allOrder=$this->getOrderNum();//总订单数量
			$sucOrder=$this->getOrderNum(5);//成功订单数量
			$per=round(($sucOrder/$allOrder),2)*100;
			return $per;
		}
		
		/*交易额*/
		function getTransNum(){
			global $db;
			$allPrice=0;
			$sql="select * from ticket_order where Tord_status=5 and Tord_cst_id=".$_SESSION['login']['userId'];
			$rs=$db->GetAllSql($sql);
			foreach($rs as $v){
				$allPrice+=$v['Tord_price3']*$v['Tord_num'];	
			}
			return $allPrice;
		}
		
		/*
		* 分页
		*/
		function page(){
			return new page($this->t);	
		}
		
		
		
	}
?>